<?php
	require_once('init.php');
	
	//Get Current Page
	if (! isset($_GET['page']) || $_GET['page'] == '') {$page = '1';} 
	else {$page = $_GET['page'];}
	
	//Isset Delete Action
	if (isset($_GET['f']) && $_GET['f'] == 'del') 
	{
		//Try Delete Column
		if (!$adminDB->executeSQL("delete from ".$util->contentdbName().".tbColumn where id='".$_GET['id']."'", $connID)) 
		{
			echo "<script>alert('类别信息删除失败！');</script>";
		}
		else
		{
			//Insert AdminLog
			if(!$adminDB->executeSQL("insert into ".$util->userdbName().".tbAdminLog (fdOperatorID,fdAction,fdTarget,fdTime) values('".$_SESSION['adminid']."', 'DELETE', '".$util->contentdbName().".tbColumn where id = ".$_GET['id']."' , now())", $connID))			
			{
				echo "<script>alert('类别信息删除成功！管理日志添加失败');</script>";
			}
			else
			{					
				echo "<script>alert('类别信息删除成功！');</script>";
			}
		}
	}
	
	//Set Shown Form Default New
	$isShow = 'N';
	
	//Isset Add Action
	if((isset($_POST['f']) && $_POST['f'] == 'add'))
	{
		//Check Form
		if(isset($_POST['name'])&&$_POST['name']!='')
		{
			//Query Column Name
			if(!$adminDB->executeSQL("select fdName from ".$util->contentdbName().".tbColumn where fdName='".trim($_POST['name'])."'",$connID))
			{			
				//Try Insert Column
				if(!$adminDB->executeSQL("insert into ".$util->contentdbName().".tbColumn (fdName,fdParentID,fdTypeID) values ('".trim($_POST['name'])."','0','1')",$connID))
				{
					echo"<script>alert('类别添加失败！');</script>";
				}
				else
				{
					//Insert AdminLog
					$adminDB->executeSQL("insert into ".$util->userdbName().".tbAdminLog (fdOperatorID,fdAction,fdTarget,fdTime) values('".$_SESSION['adminid']."','INSERT','".$util->contentdbName().".tbColumn(fdName,fdParentID,fdTypeID) values (".trim($_POST['name']).",0,1)',now())",$connID);
					echo"<script>alert('类别添加成功！');</script>";
				}
			}
			else
			{
				echo"<script>alert('已存在同名类别！请保证类别名不重复');</script>";
			}
		}
	}
	
	//Isset Edit Action
	if (isset($_GET['f']) && $_GET['f'] == 'edit'|| (isset($_POST['f']) && $_POST['f'] == 'edit')) 
	{
		//Get Current ColumnID
		if (isset($_GET['id'])) 
		{
			$id = $_GET['id'];
		}
		else 
		{
			$id = $_POST['id'];
		}
		//Confirm Post
		if (isset($_POST['f']) && $_POST['f'] == 'edit') 
		{
			if(!$adminDB->executeSQL("select fdName from ".$util->contentdbName().".tbColumn where fdName='".trim($_POST['name'])."'",$connID))
			{
				//Try Update Column
				if (! $adminDB->executeSQL("update ".$util->contentdbName().".tbColumn set fdName='".trim($_POST['name'])."' where id='".$id."'", $connID)) 
				{
					echo "<script>alert('类别信息更改失败！');</script>";
				}
				else 
				{
					//Insert AdminLog
					if(!$adminDB->executeSQL("insert into ".$util->userdbName().".tbAdminLog (fdOperatorID,fdAction,fdTarget,fdTime) values('".$_SESSION['adminid']."', 'UPDATE', '".$util->contentdbName().".tbColumn set fdName=".trim($_POST['name'])." where id=".$id."' , now())", $connID))
					{
						echo "<script>alert('类别信息更改成功！管理日志添加失败');</script>";
					}
					else
					{			
						echo "<script>alert('类别信息更改成功！');</script>";
					}
				}
			}
			else
			{
				echo"<script>alert('已存在同名类别！请保证类别名不重复');</script>";
			}
		}	
		
		//Query Current Column
		$column = $adminDB->executeSQL("select u.id, u.fdName, u.fdOrder from ".$util->contentdbName().".tbColumn u where id='".$id."'", $connID);
		//Assign Current Column
		$smarty->assign('column', $column);
		//Set Shown Form Edit
		$isShow = 'E';
	}
	//Query Column List	
	$sql = "select id,fdName from ".$util->contentdbName().".tbColumn order by fdOrder";
	$columns = $pageDB->pageData($sql, $connID, 20, $page);		
	$smarty->assign('columns', $columns);
	$smarty->assign('isShow', $isShow);
	$smarty->display('admin_listcolumn.html');
	
	$connDB->closeConnID();
	
?>